﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using GalaSoft.MvvmLight.Messaging;
using KouSuan.Classes;

namespace KouSuan.Controls
{
    /// <summary>
    /// ShowResultControl.xaml 的交互逻辑
    /// </summary>
    public partial class ShowResultControl : UserControl
    {
        #region 字段

        private Random random;

        private NotificationMessageWithCallback completeAction;

        #endregion 字段

        #region 依赖属性

        public bool? IsRight
        {
            get { return (bool?)GetValue(IsRightProperty); }
            set { SetValue(IsRightProperty, value); }
        }

        public static readonly DependencyProperty IsRightProperty =
            DependencyProperty.Register("IsRight", typeof(bool?), typeof(ShowResultControl), new UIPropertyMetadata(null));

        #endregion 依赖属性

        public ShowResultControl()
        {
            InitializeComponent();
            random = new Random();
            this.RegMsg();
        }

        #region 事件

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            GetNotifyContent();
        }

        /// <summary>
        /// 完成播放
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void meSound_MediaEnded(object sender, RoutedEventArgs e)
        {
            if (this.completeAction != null)
            {
                this.completeAction.Execute();
            }
        }

        #endregion 事件

        /// <summary>
        /// 获取提示内容
        /// </summary>
        private void GetNotifyContent()
        {
            Uri uri = null;
            Uri soundUri = null;
            string text = null;
            switch (IsRight)
            {
                case null:
                    uri = new Uri("/Resources/img/ok3.png", UriKind.Relative);
                    text = "加油哦！";
                    break;
                case true:
                    uri = new Uri("/Resources/img/ok1.png", UriKind.Relative);
                    text = "你真棒！";
                    soundUri = new Uri(System.IO.Path.Combine(Environment.CurrentDirectory, "Resources", "Sound", "guzhang.mp3"));
                    break;
                case false:
                    uri = new Uri("/Resources/img/err3.png", UriKind.Relative);
                    text = "再试一次！";
                    soundUri = new Uri(System.IO.Path.Combine(Environment.CurrentDirectory, "Resources", "Sound", "wuya.mp3"));
                    break;
                default:
                    break;
            }

            this.imageFace.Source = new BitmapImage(uri);
            this.txtWord.Text = text;
            this.meSound.Source = soundUri;
        }

        /// <summary>
        /// 注册
        /// </summary>
        private void RegMsg()
        {

            Messenger.Default.Register<NotificationMessageWithCallback>(this, MessageTokens.ShowResultCartoon,
                msg =>
                {
                    GetNotifyContent();
                    this.meSound.Play();
                    this.completeAction = msg;
                });
        }
    }
}
